package org.yxing.palpalgps.utils;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Iterator;

import org.yxing.palpalgps.PalPalGPSApplication;
import org.yxing.palpalgps.R;

import android.content.Context;
import android.util.Log;



public final class Helper {
    
    private static final String KEY = "wq8@#asASD";
    private static final String KEY_SANDBOX = "123456";
    public static final String HOST = "palpal.luv-inc.com";
    public static final String HOST_SANDBOX = "fbapp.palpalgps.kelvin.devel.corp.memoriki.com";
    public static final String URL_APP = "?r=api/";
    public static final String URL_PICTURE = "uploads/image/";
    public static final String PICTURE_SIZE_SMALL = "80";
    public static final String PICTURE_SIZE_BIG = "320";
    public static final String REFRESH = "refresh";
    public static final String RESULT = "result";
    public static final String RESOURCE = "palpal";
    /**
     * a file name for shared preferences store
     */
    public static final String SP_FILE_USER_PROFILE = "user_profile";
    
    private static SimpleDateFormat mSimpleDateFormat = null;
    
    /**
     * 取得传输的密钥
     * @param key 密钥
     * @param content 内容
     * @param time 时间
     * @return
     */
    public static String getSig(String content, String time)
    {
        String key = (PalPalGPSApplication.SANDBOX) ? KEY_SANDBOX : KEY;
        return Utils.md5(key + content + time);
    }    
    
    /**
     * 
     * @return
     */
    public static SimpleDateFormat getSimpleDateFormat() {
        if (mSimpleDateFormat == null)
            mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        
        return mSimpleDateFormat;
    }
    
    /**
     * @return host
     */
    public static String getHost() {
        return (PalPalGPSApplication.SANDBOX) ? HOST_SANDBOX : HOST;
    }
    
    /**
     * get picture url
     * @param pid
     * @return
     */
    public static String getPictureUrl(String pid) {
        return getPictureUrl(pid, PICTURE_SIZE_BIG);
    }
    
    public static String getSmallPictureUrl(String pid) {
        return getPictureUrl(pid, PICTURE_SIZE_SMALL);
    }
    
    /**
     * get picture
     * @param pid picture id
     * @param size picture size
     * @return
     */
    public static String getPictureUrl(String pid, String size) {
        return "http://" + getHost() + "/" + URL_PICTURE + size + "." + pid;
    }
    
    /**
     * @param url app url
     * @return full app url
     */
    public static String getAppUrl(String url) {
        return "http://" + getHost() + "/" + URL_APP + url;
    }

    /**
     * exit app
     * @param killSafely
     */
    public static void exit(boolean killSafely) {
        Log.v("Helper", "exit");
        if (killSafely) {
            /*
             * Notify the system to finalize and collect all objects of the app
             * on exit so that the virtual machine running the app can be killed
             * by the system without causing issues. NOTE: If this is set to
             * true then the virtual machine will not be killed until all of its
             * threads have closed.
             */
            System.runFinalizersOnExit(true);

            /*
             * Force the system to close the app down completely instead of
             * retaining it in the background. The virtual machine that runs the
             * app will be killed. The app will be completely created as a new
             * app in a new virtual machine running in a new process if the user
             * starts the app again.
             */
            System.exit(0);
        } else {
            /*
             * Alternatively the process that runs the virtual machine could be
             * abruptly killed. This is the quickest way to remove the app from
             * the device but it could cause problems since resources will not
             * be finalized first. For example, all threads running under the
             * process will be abruptly killed when the process is abruptly
             * killed. If one of those threads was making multiple related
             * changes to the database, then it may have committed some of those
             * changes but not all of those changes when it was abruptly killed.
             */
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }
    
    public static String join(Collection<String> objs, String delimiter) {
        String join = null;
        if (objs == null || objs.isEmpty()) {
            join = "";
        } else {
            Iterator<String> iterator = objs.iterator();
            StringBuffer buffer = new StringBuffer(iterator.next());
            while (iterator.hasNext()) {
                buffer.append(delimiter).append(iterator.next());
            }
            
            join = buffer.toString();
        }
        return join;
    }
    
    public static PalPalGPSApplication getApplication(Context context) {
    	return (PalPalGPSApplication) context.getApplicationContext();
    }
}